package com.cxl.live.base;

import com.cxl.live.system.dto.UserInfo;
import com.cxl.live.util.DateUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 前端返回对象
 * @Author: ChenXueLi
 * @Date: 2018-11-10 12:29
 */
@Data
public class ResultModel implements Serializable {

    @ApiModelProperty(value = "成功与否标志", name = "success")
    private Boolean success = true;

    @ApiModelProperty(value = "返回状态码，为空则默认200.前端需要拦截一些常见的状态码如403、404、500等", name = "code")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer code;

    @ApiModelProperty(value = "系统错误编码", name = "status")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer status;

    @ApiModelProperty(value = "相关消息", name = "message")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String message;

    @ApiModelProperty(value = "相关数据", name = "data")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Object data;

    @ApiModelProperty(value = "客户端ip", name = "ip")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String ip;

    @ApiModelProperty(value = "服务器时间", name = "currentTime")
    @JsonFormat(pattern = DateUtil.DEFAULT_PATTERN, timezone = "GMT+8")
    private Date currentTime = new Date();

    @ApiModelProperty(value = "操作用户", name = "userInfo")
    private UserInfo userInfo = null;

    @ApiModelProperty(value = "消息列表", name = "infoList")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private List<String> infoList = new ArrayList<>();

    public ResultModel() {}

    public ResultModel(boolean success) {
        this.success = success;
    }

    public ResultModel(boolean success, Integer code) {
        this.success = success;
        this.code = code;
    }

    public ResultModel(boolean success, Integer code, String msg){
        this(success);
        this.code = code;
        this.message = msg;
    }

    public ResultModel(boolean success, Integer code, String msg, Object data){
        this(success);
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    /**
     * 操作成功
     */
    public void info() {
        this.info("保存成功");
    }

    /**
     * 操作成功
     */
    public void info(String message) {
        this.code = 0;
        this.message = message;
    }

    /**
     * 操作失败
     * @param code
     */
    public void error(Integer code) {
        this.error(code, "操作失败");
    }

    /**
     * 操作失败
     * @param code
     * @param message
     */
    public void error(Integer code, String message) {
        this.code = code;
        this.message = message;
    }


}
